package org.myh.excel.convert;

/**
 * ClassName: Terminal
 * Description: 将分隔符后的字符转为大写 ，如驼峰命名等
 * date: 2024/3/25 10:38
 *
 * @author Yu Han
 * @since JDK 1.8
 */
public class TerminalConvert implements Convert{

    /**
     * 使用的分隔符
     */
    private String separator = null;
    private final static int MIN_SIZE = 2;

    public TerminalConvert(String separator) {
        this.separator = separator;
    }

    @Override
    public boolean interpret(String expression) {
        String[] split = expression.split(separator);
        if(split.length >= MIN_SIZE){
            return true;
        }
        return false;
    }

    /**
     * 根据分隔符转为指定格式
     *
     * @param prefix
     * @param convert 需解析的值
     * @return 转换后的字符串
     */
    @Override
    public String convert(String prefix, String convert) {
        //先解析前缀 并去掉
        //多convert流程线转换器后期实现可配
        if (prefix != null && prefix.trim() != "") {
            convert = convert.substring(convert.indexOf(prefix) + 1 + prefix.length());
        }
        //驼峰命名转换器
        String[] split = convert.split(separator);
        StringBuilder builder = new StringBuilder();
        if(split.length >= MIN_SIZE){
            //从第二段开始解析
            for (int i = 0; i < split.length; i++) {
                if(i==0){
                    builder.append(split[i]);
                    continue;
                }
                String val = split[i];
                String head = val.substring(0, 1).toUpperCase();
                String other = val.substring(1);
                builder.append(head);
                builder.append(other);
            }
            return builder.toString();
        }
        return convert;
    }


}
